var page = sm("do_Page");
var do_App = sm("do_App");
var nf = sm("do_Notification");
var root = ui("$");
var openHelper = require("helper/open");
var communityUtil = require("helper/community");
var headerAout = ui("headerAout");
var listview = ui("listview");
var qqHttp = require("helper/http");
var userUrl = require("url/user");
var listdata = mm("do_ListData");
//var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui("headerLeft");
var honorSelect = ui("do_honor_select");
var honorSelectData = [{'text':'日榜','id':'1'},{'text':'周榜','id':'2'},{'text':'月榜','id':'3'},{'text':'发帖达人榜','id':'4'}];
var honorListData = mm("do_ListData");
honorListData.addData(honorSelectData);
honorSelect.bindItems(honorListData);
honorSelect.refreshItems();
var honorType = 0;
//改变“排行类型”

headerLeft.on("touch", function() {
  do_App.closePage()
});

//andriod返回键
page.on("back", function() {
  do_App.closePage();
});

var noData = [ {"template" : 1} ];
var pvArr=["bfbfbfFF","a0afbcff","98acc4ff","ccaaa2ff","db8c71ff","e0c75fFF","ecc609FF"];
var labelTitle = ui("labelTitle");
labelTitle.text = "荣誉榜";

// 本页面-ajax业务请求url
var httpUrl = userUrl.url.value.page;

//页面装载完成后，开始初始化工作
var communityId;
var bindOnChange = false;
page.on("loaded", function() {
	var data=page.getData();
	honorType = data.honorType;
	
	communityId=communityUtil.communityVisitId();
	honorSelect.index = 5;
	honorSelect.index = honorType;
	getFirstPageData();
	
});

//邦定数据源
listview.bindItems(listdata);
var imgIndex=1;
var pageNum=1;
function getParams(pageNum,communityId,honorTypeInner)
{
  var obj ={};
  obj.page = pageNum;
  obj.communityId = communityId;
  switch(honorTypeInner)
  {
  case 0: //day
	     obj.rankType = 0;
	     break;
  case 1: //week
	     obj.rankType = 1;
	     break;
  case 2: //week
	     obj.rankType = 2;
	     break;
  case 3: //week
	     obj.rankType = 3;
	     break;	     
  }
  return obj;
}
//全部刷新数据
function getFirstPageData(){
	pageNum = 1;
	imgIndex=1;
	var params = getParams(pageNum,communityId,honorType);
	qqHttp.post(httpUrl,params,function(data){
		listview.rebound();
		listdata.removeAll();
		var content = data.page.content;
		if(content.length){
		    for (var i = 0; i < content.length; i++) {
		    	content[i].indexText=imgIndex;
		    	var level = content[i].honorLevel;
		    	content[i].honorLevelBg = pvArr[level];
		    	if(i<3){
		    		content[i].numbgSource = "source://image/honor/num0"+imgIndex+".png";;
		    		content[i].numbgVisible = true;
		    		content[i].indexFontSize = "16";
		    		content[i].indexFontColor = "FFFFFFFF";
				}else{
		    		content[i].numbgVisible = false;
		    		content[i].indexFontSize = "20";
		    		content[i].indexFontColor = "909AA6FF";
				}
		    	imgIndex=imgIndex+1;
			}
			listdata.addData(content);
		}else{
			listdata.addData(noData);
		}
		listview.refreshItems();// 刷新item数据
		if(!bindOnChange)
		{
			bindOnChange = true;
			honorSelect.on("selectChanged",function(index){
				   honorType = index;
				   getFirstPageData();   
				});
		}	
	});
}
//刷新数据
function getNextPageData() {
	pageNum++;
	
	var params = getParams(pageNum,communityId,honorType);
	qqHttp.post(httpUrl,params,function(data){
			listview.rebound();
			if(data.page.content.length){
				var content = data.page.content;
				for (var i = 0; i < content.length; i++) {
					var level = content[i].honorLevel;
			    	content[i].honorLevelBg = pvArr[level];
			    	content[i].indexText=imgIndex;
		    		content[i].numbgVisible = false;
		    		content[i].indexFontSize = "20";
		    		content[i].indexFontColor = "909AA6FF";
			    	imgIndex=imgIndex+1;
				}
				listdata.addData(content);
			}
			listview.refreshItems();
	});
}
//下拉列表，刷新数据
listview.on("pull", function(data){
	if (data.state == 2){
		getFirstPageData();
	}
});

//上拉列表，翻页数据
listview.on("push", function(data){
	if (data.state == 2){
		//getNextPageData();
		listview.rebound();
	}
});

page.on("deleteMessage", function(data) {
	var alldata = listdata.getRange(0);
	for(var i = 0;i<alldata.length;i++){
		if(alldata[i].id==data){
			listdata.removeData([i]);
			listview.refreshItems();
			break;
		}
	}
	if(alldata.length==1){
		listdata.addData(noData);
		listview.refreshItems();
	}
})